<?php
date_default_timezone_set ('Europe/Amsterdam');
header('Cache-Control: max-age=86400');
header('Expires: '.gmdate('D, d M Y H:i:s', time()+86400).' GMT');
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
//if ($_SERVER ['REQUEST_METHOD'] === 'POST')
error_reporting (E_ALL);
session_start ();

include ('configs/cms.php');
include ('cms/classes/ePDOException.php');
////
include ('cms/classes/cFunctions.php');
////PROBLEM TO FIX: get rid of global $x; thingies
////
include ('cms/classes/cPhotoAlbum.php');
include ('cms/includes/swiftmailer/swift_required.php');
include ('cms/classes/cModel.php');
include ('cms/classes/cForm.php');
include ('cms/includes/index.php');
include ('cms/functions/index.php');
include ('cms/functions/sitemenu.php');
include ('cms/template/smarty.php');
$oSmarty 	= new Smarty_XanlandCMS ();
$photoAlbum = new cPhotoAlbum ();

if (!isset ($_SERVER ['REQUEST_URI']) || $_SERVER ['REQUEST_URI'] == '/')
{
	$sQuery	= "SELECT REPLACE(`titel`, ' ', '_') FROM `pagina` WHERE `pagina_id` = 1;";
	try
	{
		$oStmt = $pPDO -> prepare ($sQuery);
		$oStmt -> execute ();
		$oStmt -> bindColumn ('REPLACE(`titel`, \' \', \'_\')', $sTitle);
		$oStmt -> fetch (PDO::FETCH_BOUND);
		$sRequest = '/1/' . $sTitle . '.html';
	}
	catch (PDOException $e)
	{
		ePDOException :: writeMessage ($e);
	}
}
else
{
	$sRequest = $_SERVER ['REQUEST_URI'];
}

preg_match ('/(.+).html(\?(.+))?/', $sRequest, $a_sUri);

if (isset ($a_sUri [1]))
{
	$a_sUri [1] = substr($a_sUri [1] , 1);
	$a_sRequest = explode ('/', $a_sUri [1]);
	$a_sRequest [1] = str_replace ('.html', '', str_replace ('_', ' ', $a_sRequest [1]));
}

$sQuery = "SELECT `inhoud`, `bgimage`, `titel` FROM `pagina` WHERE `pagina_id` = :iId AND `titel` = :sTitle;";
try
{
	$oStmt = $pPDO -> prepare ($sQuery);
	$oStmt -> bindParam (':iId', $a_sRequest [0], PDO::PARAM_INT);
	$oStmt -> bindParam (':sTitle', $a_sRequest [1], PDO::PARAM_STR);
	$oStmt -> execute ();
	$oStmt -> bindColumn ('inhoud', $sContent);
	$oStmt -> bindColumn ('bgimage', $sBgImage);
	$oStmt -> bindColumn ('titel', $sTitle);
	$oStmt -> fetch (PDO::FETCH_BOUND);
	//echo $sContent; die();
	$sContent = preg_replace_callback ($_SESSION['config']['sModuleRegex'], 'cFunctions::includeModule', $sContent);
}
catch (PDOException $e)
{
	ePDOException :: writeMessage ($e);
}

if ($sContent == NULL || $sTitle == NULL || preg_match ('/<p>\s+?<\/p>/', $sContent))
{
	header("HTTP/1.0 404 Not Found");
	$sTitle = 'Pagina niet gevonden';
	$sContent = 'Mja, allemaal leuk en aardig dat je deze pagina opvraagt...<br /> Helaas ken ik de pagina niet, dus laat ik het je maar weten!';
}

$oSmarty -> assign ('sTitle', $sTitle);
$oSmarty -> assign ('sTitle2', $sTitle);
$oSmarty -> assign ('sContent', $sContent);
$oSmarty -> assign ('sBgImage', $sBgImage);
$oSmarty -> assign ('_config_url', $_CONFIG ['url']);
$oSmarty -> assign ('_config_sitename', $_CONFIG ['sitename']);
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 4);
$sGenerationTime = '<p style="text-align: center; padding: 0px; margin: 0xp; color: green;">Generated in '.$total_time.' seconds.</p>' . PHP_EOL;
$oSmarty -> assign ('sGenerationTime', $sGenerationTime);
$oSmarty -> display ('cyberweekend/index.tpl');
@require ('cms/backupdatabase.php');